package as3.component.controls
{
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;

	import as3.component.event.SizeChangeEvent;
	import as3.component.geom.IntRectangle;
	import as3.component.support.BaseToggleButton;

	public class ToggleButton extends BaseToggleButton
	{

		//按钮显示文本
		public var _textField:Label;
		protected var _label:String;
		protected var _toggleLabel:String; /**按下时的文字*/

		//文本额图标局上下左右的间距
		protected var _leftPadding:Number = 10;
		protected var _rightPadding:Number = 10;
		protected var _topPadding:Number = 3;
		protected var _bottomPadding:Number = 3;

		//最小宽度
		protected var _minWidth:Number = 50;
		//最小高度
		protected var _minHeight:Number = 26;

		protected var _upTextColor:uint;
		protected var _overTextColor:uint;
		protected var _downTextColor:uint;
		protected var _enableTextColor:uint;
		protected var _fontBold:Boolean;
		protected var _fontSize:uint;
		protected var _unSelectedFiterColor:uint;
		protected var _unUpFiltersColor:uint;
		protected var _unOverFiltersColor:uint;
		protected var _unDownFiltersColor:uint;
		protected var _unEnableFiltersColor:uint;

		protected var _upSelectTextColor:uint;
		protected var _overSelectTextColor:uint;
		protected var _downSelectTextColor:uint;
		protected var _enableSelectTextColor:uint;
		protected var _fontSelectBold:Boolean;
		protected var _fontSelectSize:uint;
		protected var _selectedFiterColor:uint;
		protected var _upFiltersColor:uint;
		protected var _overFiltersColor:uint;
		protected var _downFiltersColor:uint;
		protected var _enableFiltersColor:uint;

		protected var fiterColor:uint;

		public function ToggleButton(labelValue:String = "", toggleLabel:String = '')
		{
			_label = labelValue || "";
			_toggleLabel = toggleLabel;
			super();
		}

		override protected function initUI():void
		{
			super.initUI();

			/**20110722*/
			_uiLibrary["upImage"] = 'ChannelUp'; //"Button_upSkin";
			_uiLibrary["overImage"] = 'ChannelOver'; //"Button_overSkin";
			_uiLibrary["downImage"] = 'ChannelDown'; //"Button_downSkin";
			_uiLibrary["disabledImage"] = 'ChannelUn'; //"Button_disabledSkin";

			_uiLibrary["selectUpImage"] = 'ChannelDown'; //"Button_selectUpSkin";
			_uiLibrary["selectOverImage"] = 'ChannelDown'; //"Button_selelctOverSkin";
			_uiLibrary["selectDownImage"] = 'ChannelDown'; //"Button_selectDownSkin";
			_uiLibrary["selectDisabledImage"] = 'ChannelUn'; //"Button_selelctDisabledSkin";

			_upTextColor = _defaultSkin.toggleBtnCssTable.upTextColor || 0x000000;
			_overTextColor = _defaultSkin.toggleBtnCssTable.overTextColor || 0x000000;
			_downTextColor = _defaultSkin.toggleBtnCssTable.downTextColor || 0x000000;
			_enableTextColor = _defaultSkin.toggleBtnCssTable.enableTextColor || 0x666666;
			_fontBold = _defaultSkin.toggleBtnCssTable.fontBold || false;
			_fontSize = _defaultSkin.toggleBtnCssTable.fontSize || 12;
			_unSelectedFiterColor = _defaultSkin.toggleBtnCssTable.unSelectedFiterColor || 0x000000;
			_unUpFiltersColor = _defaultSkin.toggleBtnCssTable.unUpSelectedFiterColor;
			_unOverFiltersColor = _defaultSkin.toggleBtnCssTable.unOverSelectedFiterColor;
			_unDownFiltersColor = _defaultSkin.toggleBtnCssTable.unDownSelectedFiterColor;
			_unEnableFiltersColor = _defaultSkin.toggleBtnCssTable.unEnableSelectedFiterColor;

			_upSelectTextColor = _defaultSkin.toggleBtnCssTable.upSelectTextColor || 0x000000;
			_overSelectTextColor = _defaultSkin.toggleBtnCssTable.overSelectTextColor || 0x000000;
			_downSelectTextColor = _defaultSkin.toggleBtnCssTable.downSelectTextColor || 0x000000;
			_enableSelectTextColor = _defaultSkin.toggleBtnCssTable.enableSelectTextColor || 0x666666;
			_fontSelectBold = _defaultSkin.toggleBtnCssTable.fontSelectBold || false;
			_fontSelectSize = _defaultSkin.toggleBtnCssTable.fontSelectSize || 12;
			_selectedFiterColor = _defaultSkin.toggleBtnCssTable.selectedFiterColor || 0x000000;
			_upFiltersColor = _defaultSkin.toggleBtnCssTable.upSelectedFiterColor;
			;
			_overFiltersColor = _defaultSkin.toggleBtnCssTable.overSelectedFiterColor;
			;
			_downFiltersColor = _defaultSkin.toggleBtnCssTable.downSelectedFiterColor;
			;
			_enableFiltersColor = _defaultSkin.toggleBtnCssTable.enableSelectedFiterColor;
			;
		}

		override protected function initCom():void
		{
			super.initCom();
			setMasked(true);
			_textField = new Label();
			_textField.adaptive = true;
			_textField.bold = _fontBold;
			_textField.fontSize = _fontSize;
			_textField.selectable = false;
			_textField.color = selected ? _upSelectTextColor : _upTextColor;
			_textField.text = _label;
			addChild(_textField);
			_textField.mouseChildren = _textField.mouseEnabled = false;
		}

		override protected function creatUI():void
		{
			super.creatUI();
			contentChange();
		}

		override protected function initListener():void
		{
			addEventListener(MouseEvent.ROLL_OVER, $onRollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, $onRollOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, $onMouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, $onMouseUpHandler);
			_textField.addEventListener(SizeChangeEvent.SIZE_CHANGE, $onLabelSizeChangeHandler);
		}

		public function get textField():Label
		{
			return _textField;
		}

		public function set label(value:String):void
		{
			if (value == _label)
				return;

			_label = value;
			_textField.text = value;
			contentChange();
			reSetLabelPosition();
		}

		public function get label():String
		{
			return _textField.text;
		}

		override public function set width(value:Number):void
		{
			super.width = value;
			reSetLabelPosition();
		}

		override public function get width():Number
		{
			return super.width;
		}

		override public function set height(value:Number):void
		{
			super.height = value;
			reSetLabelPosition();
		}

		override public function get height():Number
		{
			return super.height;
		}

		override public function set enabled(value:Boolean):void
		{
			super.enabled = value;
			if (value)
			{
				_textField.color = selected ? _upSelectTextColor : _upTextColor;
			}
			else
			{
				_textField.color = _enableTextColor;
			}
		}

		override protected function updateEnable():void
		{
			super.updateEnable();
			_textField.fontSize = selected ? _fontSelectSize : _fontSize;
			_textField.bold = selected ? _fontSelectBold : _fontBold;
			if (enabled)
			{
				_textField.color = selected ? _upSelectTextColor : _upTextColor;
			}
			else
			{
				_textField.color = _enableTextColor;
			}
		}

		/**
		 *当有文本，图标或者一些可定义的参数修改时调用，重新布局
		 *
		 */
		private function contentChange():void
		{
			var textWidth:Number = 0;
			var textHeight:Number = 0;
			var offy:Number = 0;
			var offx:Number = 0;
			if (_width > 0)
			{
				width = _width;
			}
			else
			{
				var _w:Number = _textField.width + _leftPadding + _rightPadding;
				width = _w < _minWidth ? _minWidth : _w;
			}

			if (_height > 0)
			{
				height = _height;
			}
			else
			{
				var _h:Number = _textField.height + _topPadding + _bottomPadding;
				height = _h < _minHeight ? _minHeight : _h;
			}

			enabled = enabled;
		}

		private function reSetLabelPosition():void
		{
			setMaskRect(new IntRectangle(0, 0, simpleButton.width, simpleButton.height));
			_textField.x = (this.width - _textField.width) * 0.5;
			_textField.y = (this.height - _textField.height) * 0.5;
		}

		protected function $onRollOverHandler(evt:MouseEvent):void
		{
			_textField.color = selected ? _overSelectTextColor : _overTextColor;
			_textField.fontSize = selected ? _fontSelectSize : _fontSize;
			_textField.bold = selected ? _fontSelectBold : _fontBold;
			fiterColor = selected ? _unOverFiltersColor : _overFiltersColor;
			_textField.filters = [new GlowFilter(fiterColor, 1, 2, 2, 12, 1, false, false)];
		}

		protected function $onRollOutHandler(evt:MouseEvent):void
		{
			_textField.color = selected ? _upSelectTextColor : _upTextColor;
			_textField.fontSize = selected ? _fontSelectSize : _fontSize;
			_textField.bold = selected ? _fontSelectBold : _fontBold;
			fiterColor = selected ? _unUpFiltersColor : _upFiltersColor;
			_textField.filters = [new GlowFilter(fiterColor, 1, 2, 2, 12, 1, false, false)];
		}

		protected function $onMouseDownHandler(evt:MouseEvent):void
		{
			_textField.color = selected ? _downSelectTextColor : _downTextColor;
			_textField.fontSize = selected ? _fontSelectSize : _fontSize;
			_textField.bold = selected ? _fontSelectBold : _fontBold;
			fiterColor = selected ? _unDownFiltersColor : _downFiltersColor;
			_textField.filters = [new GlowFilter(fiterColor, 1, 2, 2, 12, 1, false, false)];
		}

		override protected function $onMouseUpHandler(evt:MouseEvent):void
		{
			super.$onMouseUpHandler(evt);
			_textField.color = selected ? _overSelectTextColor : _overTextColor;
			_textField.fontSize = selected ? _fontSelectSize : _fontSize;
			_textField.bold = selected ? _fontSelectBold : _fontBold;
			fiterColor = selected ? _unUpFiltersColor : _upFiltersColor;
			_textField.filters = [new GlowFilter(fiterColor, 1, 2, 2, 12, 1, false, false)];
		}

		private function $onLabelSizeChangeHandler(evt:SizeChangeEvent):void
		{
			contentChange();
			reSetLabelPosition();
		}

		public function set upTextColor(value:uint):void
		{
			_upTextColor = value;
		}

		public function set overTextColor(value:uint):void
		{
			_overTextColor = value;
		}

		public function set downTextColor(value:uint):void
		{
			_downTextColor = value;
		}

		public function set enableTextColor(value:uint):void
		{
			_enableTextColor = value;
		}

		override public function set selected(value:Boolean):void
		{
			super.selected = value;
			if (selected)
			{
				if (_toggleLabel.length > 0)
					_textField.text = _toggleLabel;
			}
			else
				_textField.text = _label;
		}
	}
}
